﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLL
{
    //接收的数据处理 类
    public class recDataDeal
    {
        
        timeConverty b_timeConverty = new timeConverty();               //实例化时间转换类
        DAL.DBbase d_DBbase = new DAL.DBbase();                         //实例化数据库增删改查类
        BLL.addArea b_addArea = new BLL.addArea();                      //判断是否为新的区域
        
        
        public string macActionMsg { get; set; }            //设备上下线操作属性

        static string end, head;                            //头尾码
        static string temp, hum, ill, soilHum, rain;        //设置收到的数据为变量，自动模式方便判断
        static string area, facilitiesNo;

        #region 接收指令为 设备上下线指令 则判断其指令 +macAction(string data)
        /// <summary>
        /// 接收指令为 设备上下线指令 则判断其指令
        /// </summary>
        /// <param name="data">设备上下线指令</param>
        public void macAction(string data)
        {
            string onlineArea = data.Substring(1, 2);
            string onlineSensor = data.Substring(4, 2);
            string msg = "";
            string onlineOrOff = int.Parse(data.Substring(6, 1)) == 0 ? "掉线" : "上线";
            if (data.Contains("C"))
            {
                msg = DateTime.Now.ToString() + "   区域：" + onlineArea + " 传感器节点：" + onlineSensor + "  " + onlineOrOff;
            }

            if (data.Contains("F"))
            {
                msg = DateTime.Now.ToString() + "   区域：" + onlineArea + "   控制节点：" + onlineSensor + "  " + onlineOrOff;
            }

            //richTextBoxFacState.Text += msg + "\n";
            //this.richTextBoxFacState.SelectionStart = this.richTextBoxFacState.TextLength;
            //this.richTextBoxFacState.ScrollToCaret();
            this.macActionMsg = msg;
        }
        #endregion
        Model.modeIdAndArea m_modeIdInfo = new Model.modeIdAndArea();         //实例化modeid类，传递modeId 
        #region 数据接受处理方法
        public bool dataDeal(string data,string modeId)
        {
            //m_modeIdInfo.modeId = "1";
            //Model.modeIdAndArea m_modeIdInfo = new Model.modeIdAndArea();         //实例化modeid类，传递modeId        

            #region 设备上下线指令
            //接收指令长度为 9 或 7 则表示为设备上下线指令
            if (data.Length == 9 || data.Length == 7)
            {
                //处理方法
                macAction(data);
            } 
            #endregion

            #region 验证头尾码是否正确
            end = data.Substring(23, 2);                          //尾码
            head = data.Substring(data.IndexOf("A"), 2);          //头码
            //data.Substring(1, 1) != "Y" 下位机操作成功后返回 Y11 第二位为设备号(风扇、水泵等)
            //反馈机制尚未完善，所以先屏蔽这个返回指令
            if (head != "AA" && end != "55" && data.Substring(1, 1) != "Y")
            {
                return false;
            } 
            #endregion

            #region 接受的完整数据指令插入数据库
            string sqlComm = "INSERT INTO orderReceive(orderReceive,receTime) VALUES('" + data + "',getdate())";
            d_DBbase.ExecuteNonQuery(sqlComm); 
            #endregion

            #region 截取数据并插入数据库
            sqlComm = "INSERT INTO dealReceive (modeId,area,facilitiesNo,temperature,humidity,illumination,soilHum,rain,receiveTime)";
            string value = "VALUES(";

            area = data.Substring(3, 2);                                               //区域号
            facilitiesNo = data.Substring(data.IndexOf("C") + 1, 2);                   //设备号
            temp = data.Substring(data.IndexOf("T") + 1, 2);                           //温度
            hum = data.Substring(data.IndexOf("H") + 1, 2);                            //湿度
            ill = data.Substring(data.IndexOf("I") + 1, 3);                            //光照
            soilHum = data.Substring(data.IndexOf("S") + 1, 2);                        //土壤湿度
            //soilHum = (100 - int.Parse(soilHum)).ToString();
            rain = data.Substring(data.IndexOf("R") + 1, 1);
            value += modeId + "," + area + "," + facilitiesNo + "," + temp + "," + hum + "," + ill + "," + soilHum + "," + rain + ",";
            value += b_timeConverty.ConvertDateTimeInt(DateTime.Now) + ")";
            sqlComm += value;

            d_DBbase.ExecuteNonQuery(sqlComm); //插入数据库
            #endregion

            #region 判断区域号是否存在，若不存在则在设备表插入
            //b_addArea.jundgetArea(m_modeIdInfo.modeId, area, facilitiesNo);
            #endregion
            return true;             
        }
        #endregion
    }
}
